import Vue from 'vue'
import VueRouter from 'vue-router'
const Login=()=>import('../components/Login.vue')
const Home=()=>import('../components/Home.vue')
const Welcome=()=>import('../components/Welcome.vue')


const User=()=>import('../components/User/users.vue')
const Power=()=>import('../components/power/rights.vue')
const Roles=()=>import('../components/power/roles.vue')


const Params=()=>import('../components/GoodsCart/params.vue')
const Categroies=()=>import('../components/GoodsCart/goodsCart.vue')

const List=()=>import('../components/GoodsCart/List.vue')
const GoodsCart=()=>import('../components/GoodsCart/goodsCart.vue')
const Add=()=>import('../components/GoodsCart/Add.vue')
const Order=()=>import('../components/order/order.vue')
const Report=()=>import('../components/report/report.vue')
Vue.use(VueRouter)

  const routes = [
		{path:'/',redirect:'/login'},
  {path:'/login',component:Login},
	{path:'/home',component:Home,redirect:'/Welcome',children:[
		{path:'/welcome',component:Welcome},
		{path:'/users',component:User},
		{path:'/rights',component:Power},
		{path:'/roles',component:Roles},
		{path:'/categories',component:Categroies},
		{path:'/params',component:Params},
		{path:'/goods',component:List},
		{path:'/goods/add',component:Add},
		{path:'/orders',component:Order},
		{path:'/reports',component:Report}]},


		
]

const router = new VueRouter({
  routes,
	mode:'history'
})
//挂载路由导航守卫
//to将要访问的路径
//from代表从那个路径跳转
//next 放行函数
//next（'/login'）强行跳转
router.beforeEach((to,from,next)=>{
	if(to.path==='/login') return next();
	//获取token
	const tokenStr=window.sessionStorage.getItem('token')
	
	if(!tokenStr) return next('/login');
	next()
})
export default router
